//
//  RBProfileChangeNameVC.m
//  CharmZone
//
//  Created by Lan on 2018/9/13.
//  Copyright © 2018年 SummerTea. All rights reserved.
//

#import "RBPChangeTextVC.h"
#import <NSString+YYAdd.h>
#import "IQKeyboardManager.h"
#import "RBTextField.h"
#import "UIViewController+RBAdd.h"
#import "UIView+RBAdd.h"

@interface RBPChangeTextVC ()

@property(nonatomic, weak)IBOutlet UILabel *titleL;
@property(nonatomic, weak)IBOutlet RBTextField *field;

@property(nonatomic, copy) void(^completion)(NSString *text);

@end

@implementation RBPChangeTextVC

#pragma mark - Lifecycle

- (void)viewDidLoad
{
    [super viewDidLoad];
    [IQKeyboardManager sharedManager].keyboardDistanceFromTextField = 80;
    [self.field becomeFirstResponder];
}

- (void)dealloc
{
    [IQKeyboardManager sharedManager].keyboardDistanceFromTextField = 10;
}

#pragma mark - Private

- (void)dismiss
{
    [self dismissViewControllerAnimated:NO completion:nil];
}

- (void)show
{
    
}

#pragma mark - Public

- (void)showFromVC:(UIViewController *)vc
             title:(NSString *)title
       placeHolder:(NSString *)placeHolder
              text:(NSString *)text
       limitLength:(NSInteger)limitLength
        completion:(void (^)(NSString *))completion
{
    [vc presentVc:self];
    
    self.titleL.text = title;
    self.field.placeholder = placeHolder;
    self.field.text = text;
    self.field.maxTextLength = limitLength;
    self.completion = completion;
}

#pragma mark - Event

- (IBAction)confirmTapped:(id)sender
{
    NSString *text = self.field.text.stringByTrim;
    if (text.length > 0)
    {
        if (self.completion)
        {
            self.completion(text);
        }
        [self dismiss];
    }
}

- (IBAction)cancelTapped:(id)sender
{
    [self dismiss];
}

@end
